//
//  Know.m
//  Cook Street
//
//  Created by dllo on 15/8/17.
//  Copyright (c) 2015年 LANOU. All rights reserved.
//

#import "Know.h"

@implementation Know




-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
       
        
        self.knowArray = [[NSMutableArray alloc]init];
        [[DataBase KnowledgeDataBaseHandle]openKnowledgeDB];
        self.knowArray = [[DataBase KnowledgeDataBaseHandle] selectKnowledge];
        
        
        
        self.knowTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, self.frame.size.height - 64) style:UITableViewStylePlain];
        self.knowTableView.delegate = self;
        self.knowTableView.dataSource = self;
        [self.knowTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        self.knowTableView.showsVerticalScrollIndicator = NO;
        self.knowTableView .backgroundColor = [UIColor colorWithRed:233./255. green:243./255. blue:225./255. alpha:1.];
        [self addSubview:self.knowTableView];
        
        
        if (self.knowArray.count == 0) {
            [self Animating];
            [self.knowTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        }

    }
    return self;
}




#pragma mark- UItableView

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.knowArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (WIDTH - 40) / 2 + 40;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cell";
    Mine_Know_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == NO) {
        cell = [[Mine_Know_TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    cell.contentView.backgroundColor = [UIColor colorWithRed:202./255. green:230./255. blue:183./255. alpha:1.];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    KnowledgeModel *ko = [self.knowArray objectAtIndex:indexPath.row];
    [cell getValue:ko];

    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    KnowList_SecViewController *sec = [[KnowList_SecViewController alloc]init];
    [self.delegate pushController:sec];

    KnowledgeModel *k = [self.knowArray objectAtIndex:indexPath.row];
    sec.getHtml = k.html;
    sec.getTitle = k.title;
      
}






#pragma mark-   编辑UItableView

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    [self.knowArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [self setEditing:editing animated:animated];
    
    [self.knowTableView setEditing:editing animated:animated];
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle ==  UITableViewCellEditingStyleDelete) {
        
        KnowledgeModel *c = [self.knowArray objectAtIndex:indexPath.row];
        [[DataBase KnowledgeDataBaseHandle] deleteKnowledgeInfo:c.number];
        
        [self.knowArray removeObjectAtIndex:indexPath.row];
        
        [self.knowTableView reloadData];
        
    }
    
    if (self.knowArray.count == 0) {
        [self Animating];
        [self.knowTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    }
}




#pragma mark-   Animating

-(void)Animating
{
    NSMutableArray *array = [[NSMutableArray alloc]init];
    for (int i = 0; i < 28; i ++) {
        
        NSString *str = [NSString stringWithFormat:@"%d.tiff",i];
        UIImage *image = [UIImage imageNamed:str];
        [array addObject:image];
        
}
    
    
    self.img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH / 3, WIDTH / 3)];
    [self.img setCenter:CGPointMake(WIDTH / 2, HEIGHT / 2.5)];
    [self addSubview:self.img];
    
    [self.img setAnimationImages:array];
    [self.img setAnimationDuration:2];
    [self.img startAnimating];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH / 3,   HEIGHT / 3 + HEIGHT / 5, WIDTH / 2 , WIDTH / 10)];
    label.text = @"快快收藏喜欢的知识吧";
    label.font = [UIFont systemFontOfSize:16];
    [self addSubview:label];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
